<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Upload;
class LinkController extends Controller {
    public function lst(){
    	$link = D('link'); // 实例化link对象
    	$count      = $link->count();// 查询满足要求的总记录数
    	$Page       = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
    	$show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
    	$list = $link->order('li_id asc')->limit($Page->firstRow.','.$Page->listRows)->select();
    	$this->assign('list',$list);// 赋值数据集$this->assign('page',$show);// 赋值分页输出
    	$this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

	public function add(){
		$link = D('link');
		if (IS_POST) {
			$data['li_title'] = I('li_title');
			$data['li_url'] = I('li_url');
			$data['li_des'] = I('li_des');
			if ($_FILES['li_pic']['tmp_name']) {
				$upload = new Upload(C('NEWUPLOAD'));// 实例化上传类
				$info = $upload->uploadOne($_FILES['li_pic']);
				if ($info) {
					$data['li_pic'] = $info['savepath'].$info['savename'];
				}
			}
			if ($link->create($data)) {
				if ($link->add()) {
					$this->success('添加成功！',U('lst'));
				}else{
					$this->error('添加失败！',U('lst'));
				}
			}else{
				$this->error($link->getError());
			}
			return;
		}
        $this->display();
    }


	public function edit($li_id){
		$link = D('link');
		$links = $link->find($li_id);
		$this->assign('links',$links);
		if (IS_POST) {
			$data['li_id'] = I('li_id'); 
			$data['li_title'] = I('li_title');
			$data['li_url'] = I('li_url');
			$data['li_des'] = I('li_des');
			$data['li_pic'] = I('li_pic');
			if ($_FILES['li_pic']['tmp_name']!='') {
				$upload = new Upload(C('NEWUPLOAD'));
				$info = $upload->uploadOne($_FILES['li_pic']);
				if ($info) {
					$data['li_pic'] = $info['savepath'].$info['savename'];
				}
			}
			if ($link->create($data)) {
				if ($link->save()) {
					$this->success('修改成功',U('lst'));
				}else{
					$this->error('修改失败！',U('lst'));
				}
			}else{
				$this->error($link->getError());
			}
			return;
		}
        $this->display();
    }


	public function del($li_id){
		$link = D('link');
		if ($link->delete($li_id)) {
			$this->success('删除成功！',U('lst'));
		}else{
			$this->error('删除失败！');
		}
        $this->display();
    }
//批量删除
	public function bdel(){
		$link = D('link');//被选中的id
		$bdel = I('bdel');
		$bdelid = implode(',', $bdel);//将数组输出为字符串
		if ($link->delete($bdelid)) {
			$this->success('批量删除成功！',U('lst'));
		}else{
			$this->error('批量删除失败！');
		}
		$this->display();
	}
   
}